var parameters = new Object();
parameters['brand'] = new Array();
parameters['price'] = new Array();
parameters['MBArchitect'] = new Array();
parameters['CPUSlot'] = new Array();
parameters['maxMem'] = new Array();
var sortOrder, pageAmount, pageNum;

function turnPage(event) {
	var a = $(event.target);
	var text = a.text();
	if (text != pageNum) {
		if (text == '<上一页') {
			pageNum--;
		} else if (text == '下一页>') {
			pageNum++;
		} else {
			pageNum = text;
		}
		loadPage();
	}
}

function loadPage() {
	var url = BASEPATH + '/mainBoardSearch.do?';
	for (var v in parameters) {
		var list = parameters[v];
		if (list.length != 0) {
			url = url + 'selectedConditions[' + v + ']=';
			for (var i in list) {
				url = url + list[i] + ',';
			}
			url = url.substring(0, url.length-1) + '&';
		}
	}
	url = url + 'sortOrder=' + sortOrder + '&pageNum=' + pageNum;
	window.location.href = url;
}

$(document).ready(function() {
	/*初始化页面*/
	for (var v in parameters) {
		var list = parameters[v];
		if (list.length == 0) {
			$('.filter .all>a.'+v).addClass('selected');
		} else {
			/*-----=====浏览器控制台输出=====-----
			for (var i in list) {
				console.log(v+'['+i+']='+list[i]);
			}
			-----=========================-----*/
			$('.filter .value>a.'+v).each(function(index) {
				if (list.indexOf($(this).attr('name')) >= 0) {
					$(this).addClass('selected').append('<span></span>');
				}
			});
		}
	}
	
	$('.sort-tab a').each(function(index) {
		if ($(this).attr('name') == sortOrder) {
			$(this).addClass('selected');
		}
	});
	
	initialPageBar(pageAmount, pageNum, $('.page-bar'));
	
	/*事件监听*/
	$('.filter .all>a').click(function() {
		parameters[$(this).attr('class').split(' ')[0]] = new Array();
		pageNum = 1;
		loadPage();
	});
	
	$('.filter .value>a').click(function() {
		var list = parameters[$(this).attr('class').split(' ')[0]];
		if ($(this).hasClass('selected')) {
			list.splice(list.indexOf($(this).attr('name')), 1);
		} else {
			list.push($(this).attr('name'));
		}
		pageNum = 1;
		loadPage();
	});
	
	$('.filter .custom a').click(function() {
		$(this).parent().toggleClass('unfold');
	});
	
	$('.sort-tab a').click(function() {
		if (!$(this).hasClass('selected')) {
			sortOrder = $(this).attr('name');
			loadPage();
		}
	});
});